package com.css.masterslave.aop;

import com.css.masterslave.bean.DBContextHolder;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DataSourceAop {
    @Pointcut("!@annotation(com.css.masterslave.annotation.Master) "+
            "&& (execution(* com.css.masterslave.service..*.select*(..)) " +
            "|| execution(* com.css.masterslave.service..*.get*(..)))"
    )
    public void readPointcut() {

    }
    @Pointcut("@annotation(com.css.masterslave.annotation.Master) " +
            "|| execution(* com.css.masterslave.service..*.insert*(..)) " +
            "|| execution(* com.css.masterslave.service..*.add*(..)) " +
            "|| execution(* com.css.masterslave.service..*.update*(..)) " +
            "|| execution(* com.css.masterslave.service..*.edit*(..)) " +
            "|| execution(* com.css.masterslave.service..*.delete*(..)) " +
            "|| execution(* com.css.masterslave.service..*.remove*(..))")
    public void writePoint(){

    }
    @Before("readPointcut()")
    public void read(){
        DBContextHolder.slave();
    }
    @Before("writePoint()")
    public void write(){
        DBContextHolder.master();
    }

}
